<?php
include('includes/connection.php');
?>
<!DOCTYPE HTML>
<html lang="en_US">
<head>
    <meta http-equiv="content-type" content="text/html" charset="utf-8"/>
    <link rel="stylesheet" type="text/css" href="css/style.css" media="all"/>
    <script type="text/javascript" src="js/jquery.1.7.2.js"></script>
    <script type="text/javascript" src="js/AJAX.js"></script>
    <title>Izwebz.com - Demo Page</title>
</head>
<body>
<div id="wrapper">
    <div id="wrapUser_info">
        <table id="user_info">
            <thead>
            <tr>
                <th>Email</th>
                <th>Name</th>
            </tr>
            </thead>
            <tbody>
            <?php
            //$connection = mysqli_connect('localhost','root','123','users') or die('Could not connect to DB');
            //xac dinh bao nhieu dong
            $display = 5;
            //tinh tong so trang can hien
            if (isset($_GET['page']) && (int)$_GET['page']) {
                $page = $_GET['page'];
            } else {
                $query = "SELECT COUNT(id) from User";
                $res = mysqli_query($connection, $query) or die('Could not select users2' . mysqli_error($connection));
                $rows = mysqli_fetch_array($res, MYSQLI_NUM);
                $record = $rows[0];
                if ($record > $display) {
                    $page = ceil($record / $display);
                } else {
                    $page = 1;
                }
            }
            $start = (isset($_GET['start']) && (int)$_GET['start'] >= 0 && (int)$_GET['start'] <= $display * $page) ? $_GET['start'] : 0;
            $sql = "SELECT email,
                CONCAT_WS(' ',first_name,last_name) AS name
                FROM User
                ORDER BY id
                LIMIT $start, $display";
            $result = mysqli_query($connection, $sql) or die('Could not select email' . mysqli_error($connection));
            while ($set = mysqli_fetch_array($result, MYSQL_ASSOC)) {
                $name = $set['name'];
                $email = $set['email'];
                echo("<tr><td>$email</td><td>$name</td></tr>");
            }
            ?>
            </tbody>
        </table>
    </div>
    <ul class="nav">
        <?php
        if ($page > 1) {
            $next = $start + $display;
            $prev = $start - $display;
            $current = ($start / $display) + 1;

//            if ($current != 1) {
//                echo "<li><a href='index.php?start=$prev&page=$page'>Previous</a></li>";
//            }
            if ($current != 1) {
                echo "<li><a href='javascript:changePage($prev,$page,$display);'>Previous</a></li>";
            }

            for ($i = 1; $i <= $page; $i++) {
                if ($current != $i) {
                    //echo "<li><a href='index.php?start=" . $display * ($i - 1) . "&page=$page'>$i</a></li>";
                    echo "<li><a href='javascript:changePage(" . $display * ($i - 1) . ",$page,$display);'>$i</a></li>";
                } else {
                    echo "<li class='current'>$i</li>";
                }
            }
//            if ($current != $page) {
//                echo "<li><a href='index.php?start=$next&page=$page'>Next</a></li>";
//            }

            if ($current != $page) {
                echo "<li><a href='javascript:changePage($next,$page,$display);'>Next</a></li>";
            }
        }
        ?>
    </ul>
</div>
</body>
</html>